import wx

class TXPanel(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id)
        self.app = wx.GetApp()


        # 放置可控文本，用来显示内容
        vbox = wx.BoxSizer(wx.VERTICAL)
        self.text_result = wx.TextCtrl(self, -1, style=wx.TE_MULTILINE | wx.TE_RICH2)
        self.Bind(wx.EVT_TEXT, self.OnPaintMotion, self.text_result)
        # self.Bind(wx.EVT_TEXT, self.text_result, self.OnPaintMotion)  # Error:AssertionError OnInit returned false, exiting...
        self.Bind(wx.EVT_LEFT_UP, self.getSelStrCursor, self.text_result)
        vbox.Add(self.text_result, 1, wx.EXPAND)
        self.SetSizer(vbox)

    def getSelStrCursor(self, event):
        """
        获取选中文本和光标位置
        """
        # 获取选中文本
        sel_text = self.text_result.GetStringSelection()
        # 获取光标位置
        # cursor_pos = self.text_result.GetInsertionPoint()
        # 显示选中文本和光标位置
        # self.text_result.AppendText(f'选中文本：{sel_text}\n光标位置：{cursor_pos}\n')
        print(sel_text)


    def OnPaintMotion(self, event):
        """
        绘制文本
        """
        line = self.text_result.GetNumberOfLines()
        word_len = self.text_result.GetLineLength(line)
        value = self.text_result.GetValue()
        num = len(value.strip())
        self.app.frame.status_bar.SetStatusText(f'第{line}行{word_len}列，共{num}字', 1)